# Changed when ABI backwards compatibility is broken.
# Typically uses the major version.
SONAME = 0

CXXFLAGS+=-g -O2 -Werror
override CXXFLAGS+=-std=c++0x -Wall -fPIC

all: libelf++.a libelf++.so libelf++.so.$(SONAME) libelf++.pc

SRCS := elf.cc mmap_loader.cc to_string.cc
HDRS := elf++.hh data.hh common.hh to_hex.hh
CLEAN :=

libelf++.a: $(SRCS:.cc=.o)
	ar rcs $@ $^
CLEAN += libelf++.a $(SRCS:.cc=.o)

$(SRCS:.cc=.o): $(HDRS)

to_string.cc: enum-print.py data.hh Makefile
	@echo "// Automatically generated by make at $$(date)" > to_string.cc
	@echo "// DO NOT EDIT" >> to_string.cc
	@echo >> to_string.cc
	@echo '#include "data.hh"' >> to_string.cc
	@echo '#include "to_hex.hh"' >> to_string.cc
	@echo >> to_string.cc
	@echo 'ELFPP_BEGIN_NAMESPACE' >> to_string.cc
	@echo >> to_string.cc
	python3 enum-print.py -u --hex --no-type --mask shf --mask pf \
		-x loos -x hios -x loproc -x hiproc < data.hh >> to_string.cc
	@echo 'ELFPP_END_NAMESPACE' >> to_string.cc
CLEAN += to_string.cc

libelf++.so.$(SONAME): $(SRCS:.cc=.o)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -shared -Wl,-soname,$@ -o $@ $^
CLEAN += libelf++.so.*

libelf++.so:
	ln -s $@.$(SONAME) $@
CLEAN += libelf++.so

# Create pkg-config for local library and headers. This will be
# transformed in to the correct global pkg-config by install.
libelf++.pc: always
	@(VER=$$(git describe --match 'v*' | sed -e s/^v//); \
	  echo "libdir=$$PWD"; \
	  echo "includedir=$$PWD"; \
	  echo ""; \
	  echo "Name: libelf++"; \
	  echo "Description: C++11 ELF library"; \
	  echo "Version: $$VER"; \
	  echo "Libs: -L\$${libdir} -lelf++"; \
	  echo "Cflags: -I\$${includedir}") > $@
CLEAN += libelf++.pc

.PHONY: always

PREFIX?=/usr/local

install: libelf++.a libelf++.so libelf++.so.$(SONAME) libelf++.pc
	install -d $(DESTDIR)$(PREFIX)/lib/pkgconfig
	install -t $(DESTDIR)$(PREFIX)/lib libelf++.a
	install -t $(DESTDIR)$(PREFIX)/lib libelf++.so.$(SONAME)
	install -t $(DESTDIR)$(PREFIX)/lib libelf++.so
	install -d $(DESTDIR)$(PREFIX)/include/libelfin/elf
	install -t $(DESTDIR)$(PREFIX)/include/libelfin/elf common.hh data.hh elf++.hh
	sed 's,^libdir=.*,libdir=$(PREFIX)/lib,;s,^includedir=.*,includedir=$(PREFIX)/include,' libelf++.pc \
		> $(DESTDIR)$(PREFIX)/lib/pkgconfig/libelf++.pc

clean:
	rm -f $(CLEAN)

.DELETE_ON_ERROR:
